package com.littlebee.storychild.common.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;

import com.littlebee.storychild.common.BaseApplication;
import com.littlebee.storychild.common.utils.sharePrefUtils.SharePrefUtil;

import java.io.File;
import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 作者：liaoyong on 2016/7/13 08:14
 * 邮箱：liaoyong@tuliu.com
 * 描述：通用工具类，提供一些公共方法。
 */
public class Util {
    /**
     * 判断SD卡是否mounted
     *
     * @return isMounted boolean 已挂载SDCard返回true，否则返回false
     */
    public static boolean isMounted() {
        boolean isMounted = Environment.getExternalStorageState()
                .equals(Environment.MEDIA_MOUNTED);
        return isMounted;
    }

    /**
     * SD卡管理器 获取Path
     *
     * @return SDCardPath String 已挂载SDCard返回SDCard路径，否则返回"/"(根路径)
     */
    public static String getSDCardPath() {
        String SDCardPath = "";
        if (isMounted()) {
            SDCardPath = Environment.getExternalStorageDirectory().getPath();
            return SDCardPath + File.separator;
        }

        SDCardPath = DevMountUtil.getInstance().getInternalSDCardPath();
        if (SDCardPath != null && !SDCardPath.equals("")) {
            return SDCardPath + File.separator;
        }

        SDCardPath = Constant.kRootPath + File.separator;
        return SDCardPath;
    }

    /*path manager*/
    public static String getApplicationRootPath() {
        String appRootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator;
        try {
            LogUtil.d("obtain app path error1");
            if (FileUtil.mkDirs(appRootPath)) {
                return appRootPath;
            } else {
                LogUtil.d("obtain app path error1");
                return "/";
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtil.d("obtain app path error2");
            return "/";
        }
    }

    /**
     * 返回日志根路径文件夹
     *
     * @return
     * @throws MalformedURLException
     */
    public static String getLogRootPath() {
        String rootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator + "log" + File.separator;
        try {
            if (FileUtil.mkDirs(rootPath)) {
                return rootPath;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 返回日志根路径文件夹
     *
     * @return
     * @throws MalformedURLException
     */
    public static String getPhotoRootPath() {
        String rootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator + "photo" + File.separator;
        try {
            if (FileUtil.mkDirs(rootPath)) {
                return rootPath;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 返回视频根路径文件夹
     *
     * @return
     * @throws MalformedURLException
     */
    public static String getVideoRootPath() {
        String rootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator + "video" + File.separator;
        try {
            if (FileUtil.mkDirs(rootPath)) {
                return rootPath;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 返回下载文件根路径文件夹
     *
     * @return
     * @throws MalformedURLException
     */
    public static String getDownloadRootPath() {
        String rootPath = getSDCardPath() + Constant.kParentDirName
                + File.separator + "download" + File.separator;
        try {
            if (FileUtil.mkDirs(rootPath)) {
                return rootPath;
            } else {
                return "";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static boolean isStringValid(String str) {
        return str != null && !str.trim().equals("");
    }

    public static boolean isFilePathValid(String filePath)
            throws MalformedURLException {
        boolean isFilePathValid = false;
        filePath = filePath.replaceAll("\\\\", "/").trim();
        Pattern p = Pattern.compile("(^\\.|^/|^[a-zA-Z])?:?/.+(/$)?");
        Matcher m = p.matcher(filePath);
        // 不符合要求直接返回
        if (!m.matches()) {
            Log.v("", "文件路径无效！");
            throw new MalformedURLException("文件路径无效！");
        } else {
            isFilePathValid = true;
        }
        return isFilePathValid;

    }



//
//    public static AccountInfo getAccountInfo() {
//        AccountInfo accountInfo = (AccountInfo) SharePrefUtil.getInstance(BaseApplication.getInstance()).getObject(SharePrefUtil.kAccountInfo);
//        return accountInfo;
//    }
//
    public static boolean isLogin() {
//        User user = getUserInfo();
        String token = SharePrefUtil.getInstance(BaseApplication.getInstance()).getStringValue(SharePrefUtil.kToken);
        if (TextUtils.isEmpty(token) && token.length() < 6) {
            return false;
        }
        return true;
    }
    //user info clean
    //1.clean db info
    public static void clearUserInfo() {
        SharePrefUtil.getInstance(BaseApplication.getInstance()).setObject(SharePrefUtil.kUserInfo, null);
    }
//
//    private static void cleanDBInfo() {
//        //permission info
//        FinalDb finalDb = FinalDb.create(BaseApplication.getInstance());
//        finalDb.deleteAll(PermissionBean.class);
//    }
}
